package com.dongdongshop.controller;

//品牌

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.data.Page;
import com.dongdongshop.pojo.brand.TbBrand;
import com.dongdongshop.service.Brand.BrandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("brand")
public class BrandController {

    @Reference
    private BrandService brandService;

    private Logger logger = LoggerFactory.getLogger(BrandController.class);

    /**
     * 跳转品牌管理页
     */
    @RequestMapping("toBrandList")
    public String toBrandList(){
        return "brand/brand";
    }

    /*
    * 分页获取所有品牌信息
    * */
    @RequestMapping(value = "getBrandInfo",method = RequestMethod.POST)
    @ResponseBody
    public DataResult getBrandInfo(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "5")Integer pageSize){
        Page<TbBrand> page = null;
        try {
            page = brandService.getBrandInfo(pageNum,pageSize);
        }catch (Exception e){
            logger.error("根据品牌id修改品牌信息异常",e);
            return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),page);
    }

    /**
     * 跳转增加品牌页面
     */
    @RequestMapping("toAddBrandPage")
    public String toAddBrandPage(){
        return "brand/add";
    }

    /*
    * 增加品牌数据
    * */
    @RequestMapping(value = "addBrand",method = RequestMethod.POST)
    @ResponseBody
    public DataResult addBrand(TbBrand b){
        try {
            brandService.addBrand(b);
        }catch (Exception e){
            logger.error("增加品牌信息异常", e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }
    /*
    * 跳转修改品牌页，回显
    * */
    @RequestMapping("toUpdateBrandPage")
    public String toUpdateBrandPage(Long id, Model model){
        try {
            TbBrand b = brandService.getBrandById(id);
            model.addAttribute("b",b);
        }catch (Exception e){
            logger.error("跳转修改品牌页异常",e);
        }
        return "brand/update";
    }
    /*
    * 根据品牌id修改品牌信息
    * */
    @RequestMapping(value = "updateBrandById",method = RequestMethod.POST)
    @ResponseBody
    public DataResult updateBrandById(TbBrand b){
        try {
            brandService.updateBrandById(b);
        }catch (Exception e){
            logger.error("根据品牌id修改品牌信息异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }

    /**
     * 根据品牌id删除品牌数据
     */
    @RequestMapping(value = "deleteBrandById",method = RequestMethod.POST)
    @ResponseBody
    public DataResult deleteBrandById(Long id){
        try {
            brandService.deleteBrandById(id);
        }catch (Exception e){
            logger.error("根据品牌id删除品牌数据异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }

}
